/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
jQuery(document).ready(function(){
    
    /****Slide show****/
    var autoSlider = jQuery('#auto-slider').text();//Yes: Tu dong slider | No: Khong tu dong slider
    var sliderSpeed = jQuery('#slider-speed').text();//Thoi gian auto slider
    
    //Show default slide
    activeSlideByRelIndex(1);
        
    if(jQuery('#slide-news').length > 0 && autoSlider == 'Yes') {
            
        //auto play
        var idx = 1;//idx begin with 1 because has show slide default with idx==1
        var maxIdx = 5;
        var refreshIntervalId = setInterval(function(){
            if(idx < maxIdx)
                idx = idx + 1;
            else{
                idx = 1;
            }
            activeSlideByRelIndex(idx, 800);   
        }, sliderSpeed);
    }
    
   //play when hover
   jQuery('#slide-news .wrap-item').click(function(){
       jQuery('#slide-news .wrap-item').removeClass('active');
       jQuery(this).addClass('active');
       
       //get rel value
       var relValue = jQuery(this).attr('rel');
//       alert(relValue);
       
       //show big image
       if(relValue != undefined){
        if(jQuery('#slide-news .big-image-view-inner[rel="' + relValue + '"]').length > 0) {
            //hide all big images
            jQuery('#slide-news .big-image-view-inner').hide(0);
            //show big image
            jQuery('#slide-news .big-image-view-inner[rel="' + relValue + '"]').fadeIn(300);
//            jQuery('#slide-news .big-image-view-inner[rel="' + relValue + '"]').show(0);
        }
            
       }
       
       
   }); 
   
   /***End slide show***********/
   
   /*Tabs*/
   jQuery('#tabs li.tab').click(function(){
      
      var activeId = jQuery(this).attr('rel');
       //ACTIVE and DEACTIVE tab
       jQuery('#tabs .tab').removeClass('active')
       jQuery(this).addClass('active');
       
       //ACTIVE and DEACTIVE tab-content
       jQuery('#tabs .tab-content-item').css('display', 'none');
       jQuery('#' + activeId).css('display', 'block');
   });
   
   //hide all channel
   jQuery('#tabs .channel-item').hide();
   //Show channel default
   if(jQuery('#select-channel').length > 0) {
      var channelId = jQuery('#select-channel').val();
      jQuery('#' + channelId).show();
   }
   
   
   //show channel when change value
   jQuery('#select-channel').change(function(){
       ////hide all channel
       jQuery('#tabs .channel-item').hide();
       var channelId = jQuery(this).val();
       jQuery('#' + channelId).show();
   });
   /*End tabs*/
   
   /*Tv online*/
   jQuery('#radio-online').click(function(){     
      jQuery('#tv-online-img').hide();
      jQuery('#radio-animated-img').hide();
      if(jQuery('#new_player_online_wrapper').length > 0){
          jQuery('#new_player_online_wrapper').hide();
      }
      if(jQuery('#new_player_online').length > 0){
          jQuery('#new_player_online').hide();
      }
      
      //view player
//      viewChannel('wrap-player-online', 366, 25, 'mms://123.30.141.4/radio',1,'');
//      viewChannel('wrap-player-online', 366, 50, 'mms://123.30.141.4/radio',1,'');
      //viewChannel('old_player_online', 366, 50, 'mms://123.30.141.4/radio',1,'');
      viewRadioChannel('old_player_online', 366, 24);
      jQuery('#radio-animated-img').show();
      
   });
   jQuery('#tv-online, #tv-online-img').click(function(){      
      jQuery('#tv-online-img').hide();
      jQuery('#radio-animated-img').hide();
      if(jQuery('#old_player_online').length > 0){
          jQuery('#old_player_online').html('');
      }
      //view player
      //viewChannel('wrap-player-online', 366, 316, 'mms://123.30.141.4/rtv',1,'');
      
      
      thtg_get_new_player_tivionline();
   });
   
   //Add image home to menu
   imgHome = baseUrl.url + '/wp-content/themes/thtg/images/home.png';
   jQuery('.home-menu a').html('<img class="img-home" src="' + imgHome + '" />');

    //SHow home date time
    show_date_time_home();
   
   //render video player in single page
    jQuery('.thtg-video-player').each(function(){
        var id = jQuery(this).attr('id');
        var filePath = jQuery(this).html();
        filePath = decodeURIComponent(filePath.replace(/\+/g,  " "));
        var flashplayerPath = jQuery('#flashplayer-path').html();
        if(flashplayerPath) {
            jwplayer(id).setup({
                flashplayer: flashplayerPath,
                file: filePath,
                width:655,
                height:400,
                overstretch:"fit",
                smoothing:true,
                controlbar: 'bottom',
                autostart: false
            });
        }
        
    });
    
   //render video player in widget video    
    //click-play
    jQuery('.click-play').click(function(e){
       
       e.preventDefault();
       var id = jQuery(this).attr('rel');
       var filePath = jQuery(this).attr('href');
       var flashplayerPath = jQuery('#flashplayer-path').html();
       
       jwplayer(id).setup({ 
           file: filePath,
           flashplayer: flashplayerPath,
           width:655,
           height:400,
           overstretch:"fit",
           smoothing:true,
           controlbar: 'bottom',
           autostart: false
       });
       jwplayer(id).play();
       
       //active / deactive
       jQuery('.playlist.item').css('background-color','');
       jQuery('.playlist.item a').css('color','#00609A');
       
       jQuery(this).css('color','#FF0000');
       jQuery(this).parent('.playlist.item').css('background-color','');
       
    });
    
    //render audio player 
    jQuery('.thtg-audio-player').each(function(){
        var id = jQuery(this).attr('id');
        var filePath = jQuery(this).html();
        var flashplayerPath = jQuery('#flashplayer-path').html();
        if(flashplayerPath) {
            jwplayer(id).setup({
                flashplayer: flashplayerPath,
                file: filePath,
                width:655,
                height:400,
//                overstretch:"fit",
//                smoothing:true,
                controlbar: 'bottom',
                autostart: false
//                plugins: {
//                    'revolt-1': {}
//                }

            });
        }
        
    });
    
    /*Show Scroll ads*/
    showScrollAds();
    
    /*Go top*/

    jQuery(window).scroll(function(){
        if (jQuery(this).scrollTop() > jQuery(window).height()/2) {
            jQuery('.gotop').fadeIn();
        } else {
            jQuery('.gotop').fadeOut();
        }
    }); 

    jQuery('.gotop').click(function(){
        jQuery("html, body").animate({ scrollTop: 0 }, 600);
        return false;
    });
   
   /*thong ke*/
   jQuery('#download-video').on('click', function() {
        ga('send', 'event', 'Tải video', 'click', 'Tải video');
   });
   jQuery('#help-tv-online').on('click', function() {
        ga('send', 'event', 'Hướng dẫn xem trực tuyến', 'click', 'Hướng dẫn xem trực tuyến');
   });
   jQuery('#tv-online-img').on('click', function() {
        ga('send', 'event', 'Xem trực tuyến', 'click', 'Xem trực tuyến ');
   });
   jQuery('#tv-online').on('click', function() {
        ga('send', 'event', 'Xem trực tuyến 1', 'click', 'Xem trực tuyến 1');
   });
   jQuery('#cd').click(function(e){
       e.preventDefault();
   });
});
/**
*
* idx: interger (1,2,3,..max index) 
*/
activeSlideByRelIndex = function(idx, intVal){
   
   if(jQuery('#slide-news .wrap-item[rel=' + idx + ']').length > 0) {
       //remove all active
        jQuery('#slide-news .wrap-item').removeClass('active');
        //add active
        jQuery('#slide-news .wrap-item[rel=' + idx + ']').addClass('active');

        //hide all big images
        jQuery('#slide-news .big-image-view-inner').hide(0);
        //show big image
        jQuery('#slide-news .big-image-view-inner[rel="' + idx + '"]').fadeIn(intVal);
   }   
}

viewChannel = function(id,w,h,link,iv,text){
var el = document.getElementById(id);

if (el) {
    el.innerHTML='Vui lòng chờ trong giây lát';
    var s="<embed type=\"application/x-mplayer2\" pluginspage=\"http://microsoft.com/windows/mediaplayer/en/download/\" "
    +" id=\"\" name=\"\" src=\""+link+"\" autosize=\"1\" autostart=\"1\" "
    +" clicktoplay=\"1\" displaysize=\"1\" enablecontextmenu=\"1\" volume=\"100\" enablefullscreencontrols=\"1\" "
    +" enabletracker=\"1\" mute=\"0\" playcount=\"1\" showcontrols=\"1\" showaudiocontrols=\"1\" "
    +(iv==0?" showdisplay=\"0\" showgotobar=\"0\" showpositioncontrols=\"0\" showstatusbar=\"0\" showtracker=\"0\" "
    :" showdisplay=\"0\" showgotobar=\"0\" showpositioncontrols=\"1\" showstatusbar=\"1\" showtracker=\"0\" ")
    +" height=\""+h+"\" width=\""+w+"\" />";
    el.innerHTML=s;
}
}
/**
 * Player Radio online
 * @param {type} id
 * @param {type} w
 * @param {type} h
 * @returns {undefined}
 */
viewRadioChannel = function(id,w,h){
var el = document.getElementById(id);

if (el) {
    el.innerHTML='Vui lòng chờ trong giây lát';
    
    var s='<iframe width="' + w + '" height="' + h + '" src="http://radiovietnam.vn/sharedradio.aspx?rname=tiengiang&w=366&h=24" frameborder="0"></iframe>';
    
    el.innerHTML=s;
}
}


/**
 * Hien thi ngay gio tren trang chu
 */
function show_date_time_home() {
    date = new Date;
    year = date.getFullYear();
    month = date.getMonth() + 1;
    d = date.getDate();
    day = date.getDay();
    days = new Array('CN', 'Thứ Hai', 'Thứ Ba', 'Thứ Tư', 'Thứ Năm', 'Thứ Sáu', 'Thứ 7');

    h = date.getHours();
    if (h < 10) {
        h = "0" + h;
    }

    m = date.getMinutes();
    if (m < 10) {
        m = "0" + m;
    }

    s = date.getSeconds();
    if (s < 10) {
        s = "0" + s;
    }

    var datetime = '' + days[day] + ', ' + d + '/' + month + '/' + year + ' ' + h + ':' + m; // + ':' + s + ' (GMT+7)'

    var li = '<li id="date-time-home" style="border:none;float:right;" class="menu-item menu-item-type-taxonomy menu-item-object-category"><a style="background:none;font-size:11px;font-weight:normal;" href="#">' + datetime + '</a></li>';
    
    //
    
//    if(jQuery('#menu-main-menu #date-time-home').length > 0)
//        jQuery('#menu-main-menu #date-time-home').remove();
//    
//    jQuery('#menu-main-menu').append(li);

    $('#view-time').html(datetime);

    setTimeout('show_date_time_home();', '1000');
    
    return true;
}
/**
 * Show/hide Scroll ads
 */
showScrollAds = function() {
    //Only show ads for screen >= 1280
    if (screen.width >= 1280){
        $('.advertise-scroll').show();
    }
    else {
        $('.advertise-scroll').hide();
    }
}

thtg_print_post_view = function(post_id) {
    jQuery.ajax({
        type: 'POST',
        url: baseUrl.ajaxurl,
        dataType:'json',
        data: {
                "action":"thtg_print_post_view",
//                "nonce": thtgWeatherAjax.nonce,
                "post_view_id": post_id
        },
        
        success: function(data){
        }
    });
}
/**
 * 
 */
thtg_print_thtg_count_view_page = function() {
    jQuery.ajax({
        type: 'POST',
        url: baseUrl.ajaxurl,
        dataType:'json',
        data: {
                "action":"thtg_print_thtg_count_view_page"
        },
        
        success: function(data){
            jQuery('#footer .page-view span').html(data.count);
        }
    });
}

thtg_get_new_player_tivionline = function() {   
    
    var filePath = jQuery('#filePath-tv').val();
    if(filePath != "") {
        var id = 'new_player_online';
        var flashplayerPath = jQuery('#flashplayerpath-tv').val();
        jwplayer(id).setup({
                flashplayer: flashplayerPath,
                file: filePath,
                //image: thum,
                width:655,
                height:400,
                overstretch:"fit",
                smoothing:true,
                controlbar: 'bottom',
                autostart: false,
                provider: "http"
        });
    }
    else{
        jQuery.ajax({
            type: 'POST',
            url: baseUrl.ajaxurl,
            data: {
                    "action":"thtg_get_stream_path"
            },
            success: function(filePath){
                jQuery('#filePath-tv').val(filePath);
                var id = 'new_player_online';
                var flashplayerPath = jQuery('#flashplayerpath-tv').val();                
                jwplayer(id).setup({
                        flashplayer: flashplayerPath,
                        file: filePath,
                        //image: thum,
                        width:655,
                        height:400,
                        overstretch:"fit",
                        smoothing:true,
                        controlbar: 'bottom',
                        autostart: false,
                        provider: "http"
                });
                

            }
        });
    }    
    
}